/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.business;

import android.content.Context;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.ui.components.AnimatedExpandableListView;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.entity.mainbusiness.TargetDetailsReplayBean;
import com.naizhuai.ui.entity.mainbusiness.TaskScoreBean;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.NaizhuAILog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TargetMissionReplayAdapterHolder extends AnimatedExpandableListView.AnimatedExpandableListAdapter {

    private Context mContext;
    private List<TargetDetailsReplayBean.TaskBean> missionReplay;
    private LayoutInflater inflater;
    private Map<Integer,TaskScoreBean> points;
    private StarSelectedListener mListener;


    public TargetMissionReplayAdapterHolder(Context context, List<TargetDetailsReplayBean.TaskBean> missionReplay) {
        this.mContext = context;
        this.missionReplay = missionReplay;
        inflater = LayoutInflater.from(context);
        points = new HashMap<>();
    }

    public void setPointSelectedListener(StarSelectedListener mListener) {
        this.mListener = mListener;
    }

    public void setData(List<TargetDetailsReplayBean.TaskBean> items) {
        this.missionReplay = items;
    }

    public List<TaskScoreBean> getPoints() {
        List<TaskScoreBean> beans=new ArrayList<>();
        beans.addAll(points.values());
        return beans;
    }

    @Override
    public List<TargetDetailsReplayBean.LabelsAndValue> getChild(int groupPosition, int childPosition) {
        return missionReplay.get(groupPosition).getTaskCompleteInfo();
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getRealChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        MissionItemContentHolder holder;
        List<TargetDetailsReplayBean.LabelsAndValue> items = getChild(groupPosition, childPosition);
        if (convertView == null) {
            holder = new MissionItemContentHolder();
            convertView = inflater.inflate(R.layout.adapter_expandlist_mission_content_item, parent, false);
            TextView params_0 = (TextView) convertView.findViewById(R.id.params_0);
            TextView params_1 = (TextView) convertView.findViewById(R.id.params_1);
            TextView params_2 = (TextView) convertView.findViewById(R.id.params_2);
            TextView params_3 = (TextView) convertView.findViewById(R.id.params_3);
            TextView params_4 = (TextView) convertView.findViewById(R.id.params_4);
            holder.params = new TextView[]{params_0, params_1, params_2, params_3, params_4};
            convertView.setTag(holder);
        } else {
            holder = (MissionItemContentHolder) convertView.getTag();
        }
        for (int index = 0; index < holder.params.length; index++) {
            TargetDetailsReplayBean.LabelsAndValue param = items.get(index);
            if (param == null)
                continue;
            String txt = param.getLabel() + ":" + param.getValue();
            holder.params[index].setText(txt);
        }
        return convertView;
    }

    @Override
    public int getRealChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public TargetDetailsReplayBean.TaskBean getGroup(int groupPosition) {
        return missionReplay.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return missionReplay.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        MissionItemTitleHolder holder;
        TargetDetailsReplayBean.TaskBean item = getGroup(groupPosition);
        if (convertView == null) {
            holder = new MissionItemTitleHolder();
            convertView = inflater.inflate(R.layout.adapter_expandlist_mission_title_item, parent, false);
            holder.missionThumb = (CircleImageView) convertView.findViewById(R.id.mission_charge_user_thumb);
            holder.missionTitleName = (TextView) convertView.findViewById(R.id.mission_name);
            ImageView star0 = (ImageView) convertView.findViewById(R.id.star_0);
            ImageView star1 = (ImageView) convertView.findViewById(R.id.star_1);
            ImageView star2 = (ImageView) convertView.findViewById(R.id.star_2);
            ImageView star3 = (ImageView) convertView.findViewById(R.id.star_3);
            ImageView star4 = (ImageView) convertView.findViewById(R.id.star_4);
            holder.imageviews = new ImageView[]{star0, star1, star2, star3, star4};
            convertView.setTag(holder);
        } else {
            holder = (MissionItemTitleHolder) convertView.getTag();
        }
        for (int index = 0; index < holder.imageviews.length; index++) {
            holder.imageviews[index].setOnClickListener(new StarClickListener(index,groupPosition, holder.imageviews));
        }
        ImageUtils.glideThumbViewByUrl(mContext,holder.missionThumb,missionReplay.get(groupPosition).getManagerUserInfo().getThumb());
        holder.missionTitleName.setText(item.getTaskName());

        return convertView;
    }

    public interface StarSelectedListener {
        void selectedPointTotal(int total);
    }

    class StarClickListener implements View.OnClickListener {
        private int point;
        private int groupPosition;
        private ImageView[] startImages;

        public StarClickListener(int point, int groupPosition, ImageView[] startImages) {
            this.point = point;
            this.groupPosition = groupPosition;
            this.startImages = startImages;
        }

        @Override
        public void onClick(View v) {
            for (int index = 0; index < startImages.length; index++) {
                if (index <= point)
                    startImages[index].setImageResource(R.mipmap.star_good);
                else
                    startImages[index].setImageResource(R.mipmap.star_bad);
            }
            points.put(groupPosition,new TaskScoreBean(missionReplay.get(groupPosition).getTaskId(), point + 1 + ""));
            int total=0;

            for (TaskScoreBean point:points.values()){
                total=total+Integer.parseInt(point.getScore());
            }
            mListener.selectedPointTotal(total);
        }
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }


    private static class MissionItemTitleHolder {
        CircleImageView missionThumb;
        TextView missionTitleName;
        ImageView[] imageviews;
    }

    private static class MissionItemContentHolder {
        TextView[] params;
    }


}
